package com.flying.invest.strategy.service;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.exception.AppException;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.invest.invest.TransactionType;
import com.flying.invest.strategy.model.StrategyHouseDetail;

@Service(value="StrategyHouseDetailService", desc="建仓策略明细")
public class StrategyHouseDetailService extends AbstractService{
        
	@MethodInfo("新增")
	@DaoCreate(entity=StrategyHouseDetail.ENTITY_NAME)
	public StrategyHouseDetail create(
			@Param(value=StrategyHouseDetail.USER_ID, required=true,	desc="所属用户") Long user_id,
			@Param(value=StrategyHouseDetail.TYPE_ID, required=true,	desc="策略类型") Long type_id,
			@Param(value=StrategyHouseDetail.INVEST_ID, required=true,	desc="投资项目") Long invest_id,
			@Param(value=StrategyHouseDetail.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id,
			@Param(value=StrategyHouseDetail.NUM, required=true,	desc="期数") Integer num,
			@Param(value=StrategyHouseDetail.GRID_BUY_PRICE, required=false,	desc="网格买入价格") Double grid_buy_price,
			@Param(value=StrategyHouseDetail.PRICE_INTERVAL, required=true,	desc="网格间隔比例") Double price_interval,
			@Param(value=StrategyHouseDetail.GRID_AMOUNT, required=false,	desc="网格资金") Double grid_amount,
			@Param(value=StrategyHouseDetail.GRID_QUANTITY, required=false,	desc="网格数量") Double grid_quantity
		) {
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=StrategyHouseDetail.ENTITY_NAME)
	public ActionResult update(
			@Param(value=StrategyHouseDetail.DETAIL_ID, required=true,	desc="ID") Long detail_id,
			@Param(value=StrategyHouseDetail.USER_ID, required=true,	desc="所属用户") Long user_id,
			@Param(value=StrategyHouseDetail.TYPE_ID, required=true,	desc="策略类型") Long type_id,
			@Param(value=StrategyHouseDetail.INVEST_ID, required=true,	desc="投资项目") Long invest_id,
			@Param(value=StrategyHouseDetail.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id,
			@Param(value=StrategyHouseDetail.NUM, required=true,	desc="期数") Integer num,
			
			@Param(value=StrategyHouseDetail.GRID_BUY_PRICE, required=false,	desc="网格买入价格") Double grid_buy_price,
			@Param(value=StrategyHouseDetail.PRICE_INTERVAL, required=true,	desc="网格间隔比例") Double price_interval,
			@Param(value=StrategyHouseDetail.GRID_AMOUNT, required=false,	desc="网格资金") Double grid_amount,
			@Param(value=StrategyHouseDetail.GRID_QUANTITY, required=false,	desc="网格数量") Double grid_quantity
		) {
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=StrategyHouseDetail.ENTITY_NAME)
	protected ActionResult updateByTransaction2(
			@Param(value=StrategyHouseDetail.DETAIL_ID, required=true,	desc="ID") Long detail_id,
			
			@Param(value=StrategyHouseDetail.BUY_PRICE, required=false,	desc="实际买入价格") Double buy_price,
			@Param(value=StrategyHouseDetail.BUY_QUANTITY, required=false,	desc="实际买入数量") Double buy_quantity,
			@Param(value=StrategyHouseDetail.BUY_AMOUNT, required=false,	desc="实际买入金额") Double buy_amount
		) {
		
		return null;
	}

	@MethodInfo("修改")
	@Transaction
	public ActionResult updateByTransaction(
			@Param(value=StrategyHouseDetail.DETAIL_ID, required=true,	desc="ID") Long detail_id,

			@Param(value="transaction_type", required=true,	desc="交易类型", enumClass=TransactionType.class) Integer transaction_type,
			@Param(value="price", required=true,	desc="交易价格") Double price,
			@Param(value="quantity", required=true,	desc="交易数量") Double quantity,
			@Param(value="amount", required=true,	desc="交易数量") Double amount
		) {
		if(transaction_type != TransactionType.BUY.value()) {
			throw new AppException("0101","建仓策略只能是买入交易");
		}
		return this.updateByTransaction2(detail_id, price, quantity, amount);
	}

	@MethodInfo("删除")
	@Transaction
	public ActionResult removeByStrategyId(
			@Param(value=StrategyHouseDetail.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id
		) {
		var list = this.findByStrategyId(strategy_id).getRows();
		list.stream().forEach(x -> {
			if(x.getBuyAmount() != null || x.getBuyQuantity() != null) {
				throw new AppException("0101","已经存在交易不能更新/删除明细条目");
			}
			this.removeById(x.getDetailId());
		});
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=StrategyHouseDetail.ENTITY_NAME)
	public ActionResult removeById(
			@Param(value=StrategyHouseDetail.DETAIL_ID, required=true,	desc="ID") Long detail_id
		) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=StrategyHouseDetail.ENTITY_NAME, pageable=true, modelClass=StrategyHouseDetail.class)
	public QueryResult<StrategyHouseDetail> findAll(
			@Param(value=StrategyHouseDetail.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=StrategyHouseDetail.ENTITY_NAME, pageable=false, modelClass=StrategyHouseDetail.class, osql ="order by num")
	public QueryResult<StrategyHouseDetail> findByStrategyId(
			@Param(value=StrategyHouseDetail.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=StrategyHouseDetail.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public StrategyHouseDetail findById(
		
			@Param(value=StrategyHouseDetail.DETAIL_ID, required=true,	desc="ID") Long detail_id
		) {
		return null;
	}

}